/******************************************************************************

    SlkToken.c

******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "SlkToken.h"

#define PRIVATE static
#define PUBLIC
#define INSTANCE_METHOD static

// ----------------------------------------------------------------------- 
// get 
// 
// scan next token
// ----------------------------------------------------------------------- 
                           
INSTANCE_METHOD
slk_size_t
(get) ( peeker_t *d )
{
    slk_size_t    token;                 
                          
    while ( isspace ( *d->scan_text ) ) {
        ++d->scan_text;
    }
    d->last_attribute = d->attribute;

    if ( ! *d->scan_text ) {
        token = END_OF_SLK_INPUT_;
        d->attribute = 'Y';
    } else if ( isdigit ( *d->scan_text )  ||  *d->scan_text == '.' ) {
        token = NUMBER_;
        d->attribute = atof ( d->scan_text );
        while ( isdigit ( *d->scan_text )  ||  *d->scan_text == '.' ) {
            ++d->scan_text;
        }
    } else {
        token = *d->scan_text;
        d->attribute = token;
        switch ( token ) {
            case '+' :
                token = PLUS_;
                break;
            case '-' :
                token = MINUS_;
                break;
            case '*' :
                token = STAR_;
                break;
            case '/' :
                token = SLASH_;
                break;
            case '^' :
                token = CARET_;
                break;
            case '(' :
                token = LPAREN_;
                break;
            case ')' :
                token = RPAREN_;
                break;
            case '=' :
                token = EQUAL_; 
                break;
        }   
        ++d->scan_text;
    }

    return  token;
}

// ----------------------------------------------------------------------- 
// peek
//
// scan next token without consuming it
// ----------------------------------------------------------------------- 

INSTANCE_METHOD
slk_size_t
(peek) ( peeker_t *d,
         int       level )
{
    slk_size_t     token = 0;

    printf ( "peek_token is not called in an LL(1) grammar\n" );

    return  token;
}

// ----------------------------------------------------------------------- 
// InitializePeeker
// ----------------------------------------------------------------------- 

PUBLIC
void
InitializePeeker ( peeker_t  *peeker,
                   char      *expression )
{
    peeker->scan_text = expression;
    peeker->attribute = 'X';
    peeker->last_attribute = 0.0;
            
    peeker->get = get;
    peeker->peek = peek;
}
